/**
 * Created by wangcan on 2017-10-31.
 */
var moment = require('moment');
var messageUtil = require('../../common/utils/messageUtil');
var mtOrderService = require('../../services/mtOrderService');
var ambCommisionDayService = require('../../services/commission/ambCommisionDayService');
var nodeExcel = require('excel-export');
var toolUtil = require('../../common/utils/toolUtil');

// 大使订单佣金一览
exports.ambCommisionDayList = function(req, res) {
    var body = req.body;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    ambCommisionDayService.ambCommisionDayList(body, size, index, function(err, results) {
        if (err) {
            logger.error('Exception happen when ambCommisionForDayList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when ambCommisionForDayList...'));
        }else {
            var data = {
                count: results[0]["count"],
                totalPage: results[0]["page"],
                code: results[0]["code"],
                list: results[1]
            };
            res.json(messageUtil.composeSucceededMessage(req, null, data));
        }
    });
};

// 导出大使佣金
exports.ambCommisionDayDataExport = function (req,res){
    var body = req.cparams;
    var size = -1;
    var index = -1;

    ambCommisionDayService.ambCommisionDayList(body, size, index, function(err, results) {
        if (err) {
            logger.error('Exception happen when exportAmbCommisionForDayList...' + err.message);
            res.json(messageUtil.composeFailedMessage(req, 'Exception happen when exportAmbCommisionForDayList...'));
        } else {
            if(!results[1]) {
                logger.error('Exception happen when exportOrderCommisionList...导出数据为0' + err.message);
                res.json(messageUtil.composeSucceededMessage(req, null, {}));
            }else {
                var conf={};
                conf.cols= [{
                    caption: '下单日期',
                    type: 'string'
                },{
                    caption: '大使昵称',
                    type: 'string'
                },{
                    caption: '大使手机号',
                    type: 'string'
                },{
                    caption: '总佣金',
                    type: 'string'
                },{
                    caption: '分享订单佣金',
                    type: 'string'
                },{
                    caption: '自购订单佣金',
                    type: 'string'
                },{
                    caption: '小区订单佣金',
                    type: 'string'
                },{
                    caption: '被邀请大使订单佣金',
                    type: 'string'
                },{
                    caption: '总订单数',
                    type: 'string'
                },{
                    caption: '已成团订单数',
                    type: 'string'
                },{
                    caption: '未成团订单数',
                    type: 'string'
                },{
                    caption: '已退款订单数',
                    type: 'string'
                },{
                    caption: '分享订单数',
                    type: 'string'
                },{
                    caption: '自购订单数',
                    type: 'string'
                },{
                    caption: '小区订单数',
                    type: 'string'
                },{
                    caption: '被邀请大使订单数',
                    type: 'string'
                }];
                var ambCommObjArr = results[1];
                var tempRow = [];
                if (ambCommObjArr != null && ambCommObjArr.length > 0) {
                    for (var i = 0, length = ambCommObjArr.length ; i < length; i++) {
                        var temp = [];
                        if (ambCommObjArr[i].orderDate) {
                            temp.push(ambCommObjArr[i].orderDate.toString());
                        } else {
                            temp.push("");
                        }
                        if (ambCommObjArr[i].ambNickName) {
                            temp.push(ambCommObjArr[i].ambNickName.toString());
                        } else {
                            temp.push("");
                        }
                        if (ambCommObjArr[i].ambMobilePhone) {
                            temp.push(ambCommObjArr[i].ambMobilePhone.toString());
                        } else {
                            temp.push("");
                        }
                        temp.push(ambCommObjArr[i].totalCommission.toString());
                        temp.push(ambCommObjArr[i].shareCommission.toString());
                        temp.push(ambCommObjArr[i].selfPurComm.toString());
                        temp.push(ambCommObjArr[i].communityComm.toString());
                        temp.push(ambCommObjArr[i].invitedComm.toString());
                        temp.push(ambCommObjArr[i].totalOrders.toString());
                        temp.push(ambCommObjArr[i].groupOrders.toString());
                        temp.push(ambCommObjArr[i].unGroupOrders.toString());
                        temp.push(ambCommObjArr[i].refundOrders.toString());
                        temp.push(ambCommObjArr[i].shareOrders.toString());
                        temp.push(ambCommObjArr[i].selfPurOrders.toString());
                        temp.push(ambCommObjArr[i].communityOrders.toString());
                        temp.push(ambCommObjArr[i].invitedAmbOrders.toString());

                        tempRow.push(temp);
                    }
                }

                var body = req.cparams;
                var startDateTemp = moment(body.startdate.substring(0,10)).format('YYYYMMDD').toString();
                var filename = "AmbOrderCommission_" + startDateTemp + ".xlsx";
                conf.rows = tempRow;
                var ret = nodeExcel.execute(conf);
                res.setHeader('Content-Type', 'application/vnd.openxmlformats');
                res.setHeader("Content-Disposition", "attachment; filename=" + filename);
                res.end(ret, 'binary');
            }
        }
    });
}
